﻿Imports Capa_de_Acceso_a_Datos
Imports Capa_de_Negocio


Public Class frmNotasCredito

    Public FarmaciaId As Integer
    Public NotaCreditoSeleccionada As NotaCredito = New NotaCredito()


    Private Sub frmNotasCredito_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub


    Private Sub ABMNotasCredito_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FarmaciaId = 0
        CargarCombos()
        CargarGrilla()

        With dgvNotasCredito
            ' alternar color de filas  
            .AlternatingRowsDefaultCellStyle.BackColor = Color.FloralWhite
            .DefaultCellStyle.BackColor = Color.Beige
        End With

    End Sub

    Private Sub CargarCombos()
        'Combo Año
        cboAnoNC.ValueMember = "AnoId"
        cboAnoNC.DisplayMember = "Ano"
        cboAnoNC.DataSource = Constante.obtenerAnos

        'Combo Mes 
        cboMesNC.ValueMember = "MesId"
        cboMesNC.DisplayMember = "Descripcion"
        cboMesNC.DataSource = MesDAO.comboFiltroMes
        'cboMesNC.SelectedValue = Date.Now.Month

        'Combo ObraSocialXPlan
        cboObraSocialNC.ValueMember = "ObraSocialXPlanId"
        cboObraSocialNC.DisplayMember = "Descripcion"
        cboObraSocialNC.DataSource = ObraSocialXPlanDAO.comboFiltroObraSocialXPlan

        'Combo TipoNotaCredito
        cboTipoNotaCredito.ValueMember = "TipoNotaCreditoId"
        cboTipoNotaCredito.DisplayMember = "Descripcion"
        cboTipoNotaCredito.DataSource = TipoNotaCreditoDAO.comboFiltroTipoNotaCredito

    End Sub

    Private Sub CargarGrilla()
        Try
            dgvNotasCredito.DataSource = NotaCreditoDAO.obtenerNotasCreditoXFiltro(cboObraSocialNC.SelectedValue, cboMesNC.SelectedValue, cboAnoNC.SelectedValue, FarmaciaId, cboTipoNotaCredito.SelectedValue)
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

        dgvNotasCredito.Refresh()
        'dgvNotasCredito.Columns("PresentacionAno").Width = 50
        'dgvNotasCredito.Columns("MesNombre").Width = 90
        'dgvNotasCredito.Columns("FarmaciaNombre").Width = 135
        'dgvNotasCredito.Columns("NotaCreditoCodigo").Width = 70
        'dgvNotasCredito.Columns("TipoNotaCreditoNombre").Width = 30
        'dgvNotasCredito.Columns("NotaCreditoImporte").Width = 50
        'dgvNotasCredito.Columns("Descripcion").Width = 135

        'dgvNotasCredito.Columns("NotaCreditoId").Visible = False
        'dgvNotasCredito.Columns("NotaCreditoFecha").Visible = False
        'dgvNotasCredito.Columns("TipoEnvioNotaCreditoNombre").Visible = False
        'dgvNotasCredito.Columns("TipoEnvioNotaCreditoId").Visible = False
        'dgvNotasCredito.Columns("FarmaciaId").Visible = False
        'dgvNotasCredito.Columns("PresentacionId").Visible = False
        'dgvNotasCredito.Columns("NotaCreditoPath").Visible = False
        'dgvNotasCredito.Columns("TipoNotaCreditoId").Visible = False
        'dgvNotasCredito.Columns("MesId").Visible = False
        'dgvNotasCredito.Columns("ObraSocialXPlanId").Visible = False

        'dgvNotasCredito.Columns("NotaCreditoImporte").HeaderText = "Importe"
        'dgvNotasCredito.Columns("FarmaciaNombre").HeaderText = "Farmacia"
        'dgvNotasCredito.Columns("NotaCreditoCodigo").HeaderText = "Código"
        'dgvNotasCredito.Columns("TipoNotaCreditoNombre").HeaderText = "Tipo"
        'dgvNotasCredito.Columns("PresentacionAno").HeaderText = "Año"        
        'dgvNotasCredito.Columns("MesNombre").HeaderText = "Mes"        
        'dgvNotasCredito.Columns("Descripcion").HeaderText = "Obra Social"

    End Sub

    Private Sub btnSalirNC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalirNC.Click
        Me.Dispose()
    End Sub

    Private Sub btnBuscarNC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscarNC.Click
        frmSeleccionarFarmacia.frmLlamada = Me.Name
        frmSeleccionarFarmacia.ShowDialog()
    End Sub

    Private Sub btnFiltrarNC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFiltrarNC.Click
        CargarGrilla()
    End Sub

    Private Sub btnAgregarNC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregarNC.Click
        Me.Visible = False
        frmCargaNotasCredito.ShowDialog()
    End Sub

    Private Sub btnModificarNC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificarNC.Click

        If HayFilaSeleccionada() Then
            cargarFilaSeleccionada()
            frmCargaNotasCredito.Modo = "Modificacion"
            Me.Visible = False
            frmCargaNotasCredito.ShowDialog()
        Else
            MsgBox("Seleccione primero una fila", MsgBoxStyle.Information, "Atención")
        End If
    End Sub

    Private Sub cargarFilaSeleccionada()
        NotaCreditoSeleccionada.Id = dgvNotasCredito.SelectedRows(0).Cells("NotaCreditoId").Value
        NotaCreditoSeleccionada.Codigo = dgvNotasCredito.SelectedRows(0).Cells("NotaCreditoCodigo").Value
        NotaCreditoSeleccionada.Importe = dgvNotasCredito.SelectedRows(0).Cells("NotaCreditoImporte").Value
        NotaCreditoSeleccionada.Fecha = dgvNotasCredito.SelectedRows(0).Cells("NotaCreditoFecha").Value
        NotaCreditoSeleccionada.TipoNC = dgvNotasCredito.SelectedRows(0).Cells("TipoNotaCreditoNombre").Value
        NotaCreditoSeleccionada.TipoEnvioNC = dgvNotasCredito.SelectedRows(0).Cells("TipoEnvioNotaCreditoNombre").Value
        NotaCreditoSeleccionada.Farmacia = dgvNotasCredito.SelectedRows(0).Cells("FarmaciaNombre").Value
        NotaCreditoSeleccionada.TipoEnvioNCId = dgvNotasCredito.SelectedRows(0).Cells("TipoEnvioNotaCreditoId").Value
        NotaCreditoSeleccionada.TipoNCId = dgvNotasCredito.SelectedRows(0).Cells("TipoNotaCreditoId").Value
        NotaCreditoSeleccionada.Ano = dgvNotasCredito.SelectedRows(0).Cells("PresentacionAno").Value
        NotaCreditoSeleccionada.Mes = dgvNotasCredito.SelectedRows(0).Cells("MesNombre").Value
        NotaCreditoSeleccionada.FarmaciaId = dgvNotasCredito.SelectedRows(0).Cells("IdFarmacia").Value
        NotaCreditoSeleccionada.ObraSocialXPlan = cboObraSocialNC.Text
        NotaCreditoSeleccionada.PresentacionId = dgvNotasCredito.SelectedRows(0).Cells("PresentacionId").Value
    End Sub

    Private Function HayFilaSeleccionada() As Boolean
        Dim hay As Boolean = False

        If dgvNotasCredito.SelectedRows.Count > 0 Then
            hay = True
        End If

        Return hay
    End Function

    Private Sub btnEliminarNC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminarNC.Click

        If HayFilaSeleccionada() Then
            If MsgBox("¿Desea eliminar la nota de crédito seleccionada?", MsgBoxStyle.Question) = MsgBoxResult.Ok Then
                NotaCreditoDAO.eliminarNotaCredito(dgvNotasCredito.SelectedRows(0).Cells("NotaCreditoId").Value)
                CargarGrilla()
            End If
        Else
            MsgBox("Seleccione primero una fila", MsgBoxStyle.Information, "Atención")
        End If
    End Sub

    Private Sub btnVisualizarNC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVisualizarNC.Click

        If HayFilaSeleccionada() Then
            cargarFilaSeleccionada()
            frmCargaNotasCredito.Modo = "Visualizacion"
            Me.Visible = False
            frmCargaNotasCredito.ShowDialog()            
        Else
            MsgBox("Seleccione primero una fila", MsgBoxStyle.Information, "Atención")
        End If
    End Sub

    Private Sub frmNotasCredito_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
        CargarGrilla()
    End Sub

    Private Sub btnExcelNC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExcelNC.Click        
        Me.Visible = False
        frmImportarNotasCredito.ShowDialog()
    End Sub

End Class